package controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import entity.Essay;
import entity.Notice;
import entity.Reply_suggest;
import entity.Suggest;
import service.BlogService;
import service.NoticeService;
import service.SuggestService;

@Controller
public class Blog {
	@Resource(name="blogServiceImpl")
	private BlogService blogService;
	
	@Resource(name="suggestServiceImpl")
	private SuggestService suggestService;
	
	@Resource(name="noticeServiceImpl")
	private NoticeService noticeService;
	
	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public SuggestService getSuggestService() {
		return suggestService;
	}

	public void setSuggestService(SuggestService suggestService) {
		this.suggestService = suggestService;
	}

	public BlogService getBlogService() {
		return blogService;
	}

	public void setBlogService(BlogService blogService) {
		this.blogService = blogService;
	}

	@RequestMapping("index")
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("blog");
		List<Essay> essays  = blogService.getOpenEssays();
		Collections.reverse(essays);
		mv.addObject("essays", essays);
		Notice notice = noticeService.getNotice();
		mv.addObject("notice", notice);
		return mv;
	}
	
	@RequestMapping("about")
	public ModelAndView about(){
		return new ModelAndView("about");
	}

	@RequestMapping("pay")
	public ModelAndView pay(){
		return new ModelAndView("pay");
	}
	
	@RequestMapping("suggest")
	public ModelAndView suggest(){
		ModelAndView mv =  new ModelAndView("suggest");
		List<Suggest> suggests = suggestService.getList();
		mv.addObject("suggests", suggests);
		List<Reply_suggest>reply_suggests= suggestService.getReplyList();
		
		Map<Integer,List<Reply_suggest>> map = new HashMap<Integer,List<Reply_suggest>>();
		for(Reply_suggest r:reply_suggests){
			int key = r.getReply_suggest_id();
			List<Reply_suggest> repList = map.get(key);
			if(repList!=null){
				repList.add(r);
			}else{
				List<Reply_suggest> tempList= new ArrayList<Reply_suggest>();
				tempList.add(r);
				map.put(key, tempList);
			}
		}
		mv.addObject("map", map);
		return mv;
	}
}
